package com.unknown.wificontrol.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.unknown.wificontrol.R;


public class NavTitleBar extends FrameLayout implements View.OnClickListener {


    private RelativeLayout rl_background;

    private ImageView ivBack;
    private RelativeLayout rl_back;
    private TextView tvTitle;

    private RelativeLayout rl_right;
    private TextView tvRight;
    private ImageView ivRight;


    private Context mContext;

    public NavTitleBar(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public NavTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public NavTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    //默认显示返回键
    private boolean isShowBack = true;

    //默认标题颜色
    private int titleColor;

    private int rightTextColor;

    private String titleString = "";

    private String rightText = "";
    private int rightDrawable;

    //默认背景
    private int background = 0;

    private int backgroundColor;

    private void init(Context aContext, AttributeSet attrs) {
        mContext = aContext;
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_nav_bar, this, false);
        addView(view);
        rl_background = findViewById(R.id.rl_background);
        rl_back = findViewById(R.id.rl_back);
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tv_title);
        rl_right = findViewById(R.id.rl_right);
        tvRight = findViewById(R.id.tv_right);
        ivRight = findViewById(R.id.iv_right);

        titleColor = ContextCompat.getColor(mContext, R.color.color33);
        rightTextColor = ContextCompat.getColor(mContext, R.color.color33);
        if (attrs != null) {
            TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.NavTitleBar);
            //背景
            int aBackground = typedArray.getResourceId(R.styleable.NavTitleBar_background, 0);
            if (aBackground > 0) {
                background = aBackground;
            }

            backgroundColor = typedArray.getColor(R.styleable.NavTitleBar_backgroundColor, 0);

            isShowBack = typedArray.getBoolean(R.styleable.NavTitleBar_showBack, true);

            String aTitleString = typedArray.getString(R.styleable.NavTitleBar_title);
            if (!TextUtils.isEmpty(aTitleString)) {
                titleString = aTitleString;
            }

            titleColor = typedArray.getColor(R.styleable.NavTitleBar_titleColor, titleColor);
            rightText = typedArray.getString(R.styleable.NavTitleBar_rightText);
            rightDrawable = typedArray.getResourceId(R.styleable.NavTitleBar_rightDrawable, 0);
            rightTextColor = typedArray.getColor(R.styleable.NavTitleBar_rightTextColor, rightTextColor);

            int backDrawable = typedArray.getResourceId(R.styleable.NavTitleBar_backDrawable, 0);
            if (backDrawable > 0) {
                ivBack.setImageResource(backDrawable);
            }
        }

        if (background > 0) {
            rl_background.setBackgroundResource(background);
        } else {
            if (backgroundColor > 0) {
                rl_background.setBackgroundColor(backgroundColor);
            } else {
                rl_background.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
            }
        }

        rl_back.setVisibility(isShowBack ? View.VISIBLE : View.GONE);

        tvTitle.setText(titleString);

        tvTitle.setTextColor(titleColor);

        tvRight.setTextColor(rightTextColor);

        if (!TextUtils.isEmpty(rightText)) {
            rl_right.setVisibility(View.VISIBLE);
            tvRight.setText(rightText);
        }

        if (rightDrawable > 0) {
            rl_right.setVisibility(View.VISIBLE);
            tvRight.setVisibility(View.GONE);
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setImageResource(rightDrawable);
        }

        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);
    }

    public void setTvRight(String rightText) {
        if (TextUtils.isEmpty(rightText)) {
            rl_right.setVisibility(View.GONE);
        } else {
            rl_right.setVisibility(View.VISIBLE);
            tvRight.setText(rightText);
        }
    }

    public void setRightDrawable(int rightDrawable) {
        this.rightDrawable = rightDrawable;
        rl_right.setVisibility(View.VISIBLE);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(rightDrawable);
    }

    public void setTvTitle(String title) {
        tvTitle.setText(title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back: {
                if (isShowBack) {
                    if (navBackClickListner != null) {
                        navBackClickListner.onNavBack();
                    } else {
                        if (mContext instanceof Activity) {
                            ((Activity) mContext).finish();
                        }
                    }
                }
            }
            break;
            case R.id.rl_right: {
                if (navRightClickListener != null) {
                    navRightClickListener.onRightClick();
                }
            }
            break;
        }
    }

    private OnNavBackClickListner navBackClickListner = null;

    public void setNavBackClickListner(OnNavBackClickListner navBackClickListner) {
        this.navBackClickListner = navBackClickListner;
    }

    private OnNavRightClickListener navRightClickListener = null;

    public void setNavRightClickListener(OnNavRightClickListener navRightClickListener) {
        this.navRightClickListener = navRightClickListener;
    }

    public interface OnNavBackClickListner {
        public void onNavBack();
    }

    public interface OnNavRightClickListener {
        public void onRightClick();
    }
}
